package cn.demarcia.coco.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.demarcia.coco.R;
import cn.demarcia.coco.uihelper.StringUtils;
import info.jimao.newsdk.models.HistoryModel;

/**
 * Created by lzy
 * on 2016/8/2.
 */
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {
    private Context context;
    private final LayoutInflater layoutInflater;
    private List<Object> items = new ArrayList<>();

    public HistoryAdapter(Context context, List<Object> items) {
        this.context = context;
        this.items = items;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.list_item_history, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final HistoryModel item = (HistoryModel) items.get(position);

        if (!StringUtils.isEmpty(item.des)) {
            holder.tvDescription.setText(item.des);
        }

        if (!StringUtils.isEmpty(item.title)) {
            holder.tvTitle.setText(item.title);
        }

        if (!StringUtils.isEmpty(item.pic)) {
            holder.ivPic.setImageURI(Uri.parse(item.pic));
        } else {
            holder.ivPic.setImageResource(R.drawable.ic_sentiment_dissatisfied_white_48dp);
        }
    }


    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ivPic)
        SimpleDraweeView ivPic;
        @Bind(R.id.tvTitle)
        TextView tvTitle;
        @Bind(R.id.tvDescription)
        TextView tvDescription;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
